<?php
/**
 * Controller_Ajax_Article
 *
 * Класс Controller_Ajax_Article отвечает за обработку запросов связанных со статьями
 */
class Controller_Ajax_Article extends Controller_Ajax {

	/**
	 * (non-PHPdoc)
	 *
	 */
	public function actionSwitch() {
	  Service_Debug::setDebug();

		switch ($this->arrRequest[2]) {
			case 'comment':
				$this->actionComment();
				break;
			default: $this->setError(404);
 		}
	}

	
	/**
	 * Комментарий к статье
	 *
	 */
	public function actionComment() {
		Service_Debug::setDebug();
		$text = isset($_POST['text']) ? Service_Escape::absolutelyEscapeString($_POST['text']) : '';
		$idArticle = isset($_POST['ID_article']) ? Service_Escape::absolutelyEscapeString($_POST['ID_article']) : '';
		
		if (empty($text) || empty($idArticle)) return $this->makeStandardError('Ошибка');
		$objArticle = new Article($idArticle);
		
		if(!$objArticle->id) return $this->makeStandardError('Ошибка');
		
		$objArticleComment = new ArticleComment(array('text' => $text, 'ID_article' => $idArticle));
		if($objArticleComment->save()) {
			$blockCode = '<div class="comment-block">';
			$blockCode .= '<div class="comment-info">';
			$blockCode .= '<div class="com-date">' . Service_Date::changeDateFormat($objArticleComment->getField('created_at'), 'Y-m-d H-i-s', 'j R Y в H:i') . '</div>';
			$blockCode .= '</div><div class="comment-text">' . $text . '</div></div>';
			$this->arrReturn = array('result' => 'OK', 'block' => $blockCode);
		}
	}
}
?>